﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AddIn.EA.Architecture.ComponentLogic;
using AddIn.EA.Architecture.Presentation.Interfaces;

namespace AddIn.EA.Architecture.Presentation.Controls
{
    public partial class BEProperties : UserControl, IAddNewBusinessEntity
    {
        private BusinessEntity _entity;
        private const string NewProperty = "<New Property>";

        public BEProperties()
        {
            InitializeComponent();
            this.Load += new EventHandler(BEProperties_Load);
        }

        public event Action LastTabIndex;
        public event Action FirstTabIndex;
        public event Action<bool> EntityValidated;

        private IAddNewBusinessEntity BEWizard
        {
            get
            {
                try
                {
                    return (IAddNewBusinessEntity)this.Parent.Parent.Parent;
                }
                catch { return null; }
            }
        }

        #region IAddNewBusinessEntity Members

        public BusinessEntity Entity
        {
            get
            {
                if (_entity == null && BEWizard != null)
                    _entity = BEWizard.Entity;

                return _entity;
            }
            set
            {
                _entity = value;
            }
        }

        private Common.ControlTabOrder TabOrder { get; set; }

        public void RefreshEntity()
        {
            if (BEWizard != null)
                BEWizard.RefreshEntity();
        }
        #endregion

        private void btnRemoveProperty_Click(object sender, EventArgs e)
        {
            if (propertiesBindingSource.Current != null)
            {
                DialogResult result = MessageBox.Show("Do you really want remove the property?", "Removing property confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (result == DialogResult.Yes)
                    propertiesBindingSource.RemoveCurrent();

                UpdateControlState();
                this.RefreshEntity();
                this.ValidateProperties();
            }
        }

        private void UpdateControlState()
        {
            txtName.Enabled = (this.Entity.Properties.Count > 0);
            txtNotes.Enabled = (this.Entity.Properties.Count > 0);
            cboType.Enabled = (this.Entity.Properties.Count > 0);
            chkFilterEntity.Enabled = this.Entity.HasFilter;
            chkFilterKey.Enabled = this.Entity.HasKeyFilter;
            grpFilter.Enabled = (this.Entity.Properties.Count > 0) && (this.Entity.HasFilter || this.Entity.HasKeyFilter);

            UpdatePropertyCheck();
        }

        private void UpdatePropertyCheck()
        {
            if (this.Entity.Properties.Count > 0)
            {
                if (!this.Entity.HasFilter)
                    foreach (Property property in this.Entity.Properties)
                        property.ComposeFilter = false;

                if (!this.Entity.HasKeyFilter)
                    foreach (Property property in this.Entity.Properties)
                        property.ComposeKey = false;
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            propertiesBindingSource.Add(PrepareNewProperty());
            propertiesBindingSource.MoveLast();

            this.UpdateControlState();
            this.RefreshEntity();
            this.ValidateProperties();

            txtName.Focus();
        }

        private void propertiesBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            this.RefreshEntity();
            this.ValidateProperties();
        }

        private void BEProperties_VisibleChanged(object sender, EventArgs e)
        {
            this.UpdateControlState();
        }

        private void BEProperties_Load(object sender, EventArgs e)
        {
            TabOrder = new Common.ControlTabOrder();
            TabOrder.OnLastTabIndex += new EventHandler<Common.TabIndexEventArgs>(TabOrder_OnLastTabIndex);
            TabOrder.OnFirstTabIndex += new EventHandler<AddIn.EA.Architecture.Presentation.Common.TabIndexEventArgs>(TabOrder_OnFirstTabIndex);
            TabOrder.Add(txtName);
            TabOrder.Add(cboType);
            TabOrder.Add(txtNotes);
            TabOrder.Add(chkFilterEntity);
            TabOrder.Add(chkFilterKey);
            TabOrder.Add(btnNew);
            TabOrder.Add(btnRemoveProperty);
            TabOrder.Add(dgvProperties);

            txtNotes.AcceptsTab = false;

            if (this.Entity != null && this.Entity.Properties == null)
                this.Entity.Properties = new List<Property>();

            UpdateControlState();

            propertiesBindingSource.DataSource = this.Entity.Properties;
            propertiesBindingSource.MoveLast();

            this.RefreshEntity();

            this.GotFocus += new EventHandler(BEProperties_GotFocus);
        }

        void BEProperties_GotFocus(object sender, EventArgs e)
        {
            TabOrder.MoveToFirst();
        }

        void TabOrder_OnFirstTabIndex(object sender, AddIn.EA.Architecture.Presentation.Common.TabIndexEventArgs e)
        {
            if (FirstTabIndex != null)
                FirstTabIndex();
            else
                TabOrder.MoveToLast();
        }

        private void TabOrder_OnLastTabIndex(object sender, AddIn.EA.Architecture.Presentation.Common.TabIndexEventArgs e)
        {
            if (LastTabIndex != null)
                LastTabIndex();
            else
                TabOrder.MoveToFirst();
        }

        private Property PrepareNewProperty()
        {
            Property property = new Property();
            property.Name = NewProperty;
            property.ComposeFilter = this.Entity.HasFilter;
            property.ComposeKey = false;

            return property;
        }

        private void ValidateProperties()
        {
            bool isValid = this.Entity.Properties.Count == 0
                || (txtName.Text != NewProperty && txtName.Text.Trim().Length > 0 
                    && cboType.Text.Trim().Length > 0);

            if (EntityValidated != null)
                EntityValidated(isValid);
        }

    }
}
